package com.simon.app.october.modules.sys.controllers;

import com.simon.app.october.common.annotation.SysLog;
import com.simon.app.october.common.utils.Constant;
import com.simon.app.october.common.utils.QueryPage;
import com.simon.app.october.common.utils.Result;
import com.simon.app.october.common.validator.ValidatorUtils;
import com.simon.app.october.common.validator.group.AddGroup;
import com.simon.app.october.common.validator.group.UpdateGroup;
import com.simon.app.october.modules.sys.entity.SysUserEntity;
import com.simon.app.october.modules.sys.service.SysUserRoleService;
import com.simon.app.october.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysUserRoleService userRoleService;

    /**
     * 获取所有用户的列表
     * */
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        // 只有超级管理员能查看所有管理员列表

        // 非管理员就只能获得自己创建的管理员
        if (!getUserId().equals(Constant.SUPER_ADMIN)) {
            params.put("createUserId", getUserId());
        }

        return Result.ok().put("page", userService.getPage(new QueryPage(params)));
    }

    /**
     * 获取登录用户的信息
     * */
    @GetMapping("/info")
    public Result info() {
        return Result.ok().put("user", getUser());
    }

    /**
     * 保存用户
     * */
    @PostMapping("/save")
    @RequiresPermissions("sys:user:save")
    @SysLog("新增用户")
    public Result save(@RequestBody SysUserEntity userEntity) {

        ValidatorUtils.validateEntity(userEntity, AddGroup.class);

        userEntity.setCreateUserId(getUserId());
        userService.save(userEntity);

        return Result.ok();
    }

    @GetMapping("/info/{userId}")
    public Result info(@PathVariable("userId") Long userId) {
        SysUserEntity userEntity = userService.findById(userId);

        // 查出这个人的所有角色
        List<Long> roleIdList = userRoleService.getRoleIdList(userId);

        userEntity.setRoleIdList(roleIdList);

        return Result.ok().put("user", userEntity);
    }

    @PostMapping("/update")
    @RequiresPermissions("sys:user:update")
    @SysLog("更新用户")
    public Result update(@RequestBody SysUserEntity userEntity) {

        ValidatorUtils.validateEntity(userEntity, UpdateGroup.class);

        userService.update(userEntity);

        return Result.ok();
    }

    @PostMapping("/delete")
    @RequiresPermissions("sys:user:delete")
    @SysLog("删除用户")
    public Result delete(@RequestBody List<Long> userIds) {
        if (userIds.isEmpty()) {
            return Result.error(1,"userId不能为空");
        }

        if (userIds.contains(Constant.SUPER_ADMIN)) {
            return Result.error(2,"管理员不能删除");
        }

        if (userIds.contains(getUserId())) {
            return Result.error(2,"不能删除自己");
        }

        userService.delete(userIds);
        return Result.ok();
    }
}
